package cn.cms.dao;

import cn.cms.constants.Constants;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;

import java.io.Serializable;
import java.util.List;

/**
 * Created by leon on 2017/2/14.
 */
public class PageDao<T> implements Serializable {

    public static final String ASC = "asc";
    public static final String DESC = "desc";

    public long totalCount;
    public int totalPages;
    public int pageSize = Constants.PAGE_SIZE;
    public int pageNo = 1;
    public int prePage;
    public int nextPage;
    public int firstPage = 1;
    public int lastPage;
    public int limitStart;
    public String orderBy = null;
    public String order = null;
    public List<T> result = Lists.newArrayList();
    public JSONObject queryCondition;
    public String divId;

    public String getDivId() {
        return divId;
    }

    public void setDivId(String divId) {
        this.divId = divId;
    }

    public int getFirstPage() {
        return firstPage;
    }

    public int getLastPage() {
        return this.getTotalPages();
    }


    public int getNextPage() {

        if (pageNo < this.getTotalPages()){

            nextPage = pageNo + 1;
        }else{

            nextPage = this.getTotalPages();
        }
        return nextPage;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    public String getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public int getPageNo() {
        if (this.getTotalCount() > 0) {
            pageNo = pageNo > this.getTotalPages() ? this.getTotalPages() : pageNo;
        }
        return pageNo - 1 <= 0 ? 0 : pageNo - 1;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPrePage() {

        if (pageNo > 1){

            prePage = pageNo - 1;
        }else{

            prePage = 1;
        }
        return prePage;
    }


    public List<T> getResult() {
        return result;
    }

    public void setResult(List<T> result) {
        this.result = result;
    }

    public long getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }

    public int getTotalPages() {

        if (this.getTotalCount()>0){

            int count = (int) (this.getTotalCount() / pageSize);
            if (this.getTotalCount() % pageSize > 0){

                totalPages = count + 1;
            }else{

                totalPages = count;
            }

        }else{

            totalPages = 0;
        }
        return totalPages;
    }

    public int getLimitStart() {

        if (this.getTotalCount() > 0){

            limitStart = this.getPageNo() * pageSize;
            if (limitStart >= this.getTotalCount()){

                limitStart = (int) this.getTotalCount();
            }
        }else{

            limitStart = 0;
        }

        return limitStart;
    }

    public JSONObject getQueryCondition() {
        return queryCondition;
    }

    public void setQueryCondition(JSONObject queryCondition) {
        this.queryCondition = queryCondition;
    }
}
